package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.dsp.bank.directbank.bankInterface.IUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.ReqQryUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfoDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取未到期票据信息
 * @author Parker on 2019-11-19.
 * @version 1.0
 */
public class CIB02QryUndueBillInfo extends CIB02Base implements IUndueBillInfo {
    @Override
    public RetAtsUndueBillInfo qryUndueBillInfo(ReqQryUndueBillInfo recInfo) throws Exception {
        RetAtsUndueBillInfo retAts = new RetAtsUndueBillInfo(recInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "18004");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("DLENUM", recInfo.DealerCode);//经销商代码
            xmlWriter.writeElementString("BILLNO", "");//汇票号
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),"T18004");//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String ZBS = tmpBkReader.getFirstText("/RESPONSE/ZBS");
                    if (Integer.parseInt(ZBS) > 0) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        while (tmpBkReader.read("/RESPONSE/DATA")){
                            RetAtsUndueBillInfoDetail tmpRetDetail = new RetAtsUndueBillInfoDetail();
                            tmpRetDetail.DealerCode = recInfo.DealerCode;
                            tmpRetDetail.BillNo = tmpBkReader.readedXElement.getSingleTextNotNull("BILLNO");
                            tmpRetDetail.BillMoney = tmpBkReader.readedXElement.getSingleTextNotNull("DAFAMT");
                            tmpRetDetail.BillStartDate = tmpBkReader.readedXElement.getSingleTextNotNull("STADAT").replace("-","");
                            tmpRetDetail.BillEndDate = tmpBkReader.readedXElement.getSingleTextNotNull("ENDDAT").replace("-","");
                            tmpRetDetail.BailMoney = tmpBkReader.readedXElement.getSingleTextNotNull("BALAMT");
                            tmpRetDetail.RepaymentMoney = tmpBkReader.readedXElement.getSingleTextNotNull("DECAMT");;
                            tmpRetDetail.BailAndRepaymentMoney = "";
                            tmpRetDetail.Reserve1 = "";
                            tmpRetDetail.Reserve2 = "";
                            tmpRetDetail.Reserve3 = "";
                            tmpRetDetail.Reserve4 = "";
                            tmpRetDetail.Reserve5 = "";
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = "银行返回数据为空";
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }

            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
